"""
切分句子
"""
import jieba
import string
import jieba.posseg as psg
import logging

import config
from lib.use_stopword import stop_word


# 取消打印jieba的日志信息
jieba.setLogLevel(logging.INFO)
# 加载用户词典
jieba.load_userdict(config.user_dict_path)
# 定义字母序列
letters = string.ascii_lowercase


def cut_by_word(word):
    """按单个字进行切分"""
    # python难不难,好学么? --> [python, 难， 不， 难， ， ，好， 学， 么, ?]
    # print(letters)
    result = []
    temp = ""  # 存储英文单词
    word_len = 0  # 记录当前遍历句子的长度
    for elm in word:
        word_len += 1
        if elm.lower() in letters:
            temp += elm
            if word_len == len(word) and temp:
                result.append(temp)
                temp = ""
        else:
            if temp:
                result.append(temp)
                temp = ""
            result.append(elm)

    return result


def cut(sentence, by_word=False, use_stop_word=False, with_sg=False):
    """
    切分句子
    :param sentence: str 要切分的句子
    :param by_word: 是否按单个字进行切分
    :param use_stop_word: 是否使用停用词
    :param with_sg: 是否返回词性
    :return:
    """
    if by_word:
        # print("sentence:", type(sentence))
        result = cut_by_word(sentence)
        # print("result:", result)
        # raise Exception
    else:
        # 将输入的数据按照加载字典的内容分成[pair(word, flag), pair(word, flag)...]的形状
        result = psg.lcut(sentence)
        if with_sg:  # 返回词性
            result = [(i.word, i.flag) for i in result]
        else:  # 不返回词性
            result = [i.word for i in result]

    if use_stop_word:  # 使用停用词
        if with_sg:
            result = [(i, j) for i, j in result if i not in stop_word]
        else:
            result = [i for i in result if i not in stop_word]

    return result


if __name__ == '__main__':
    sentence = "python难不难,好学么?"
    print(cut_by_word(sentence))





